﻿#include "FITKAbaFieldOutputManager.h"
#include "FITKAbaFieldOutput.h"
#include "FITKAbaAbstractStep.h"

namespace Interface
{

void FITKAbaFieldOutputManager::deleteStepFieldOutputs(int stepId)
{
    QList<FITKAbaFieldOutput*> toDelete{};
    int count = this->getDataCount();
    for (int i = 0 ; i<count; ++i)
    {
        FITKAbaFieldOutput* m = this->getDataByIndex(i);
        if (m == nullptr)
            continue;
        if (m->getCreatedStep() == stepId)
        {
            toDelete.append(m);
        }
    }
    for (FITKAbaFieldOutput* o : toDelete)
    {
        this->removeDataObj(o);
    }
}

void FITKAbaFieldOutputManager::createDefaultFieldOutput(FITKAbaAbstractStep* step)
{
    //当前的场输出个数
    int count = this->getDataCount();
    for (int i = 0; i < count; ++i)
    {
        FITKAbaFieldOutput* out = this->getDataByIndex(i);
        if (out->isValidInStep(step->getDataObjectID()))
            return;
    }
    FITKAbaFieldOutput* output = new FITKAbaFieldOutput();
    output->setParent(this);
    QString name = this->checkName("F-Output-1");
    output->setDataObjectName(name);
    output->setCreatedStep(step->getDataObjectID());
    this->appendDataObj(output);
    if (step->isPerturbation())
        output->setOutputFrequency(FITKAbaOutputVariable::OutputFrequency::AllModes);
}

void FITKAbaFieldOutputManager::replaceStep(int oldStep, int newStep)
{
    int count = this->getDataCount();
    for (int i = 0; i < count; ++i)
    {
        FITKAbaFieldOutput* data = this->getDataByIndex(i);
        if (data == nullptr)
            continue;
        if (data->getCreatedStep() != oldStep)
            continue;
        data->setCreatedStep(newStep);
        if (!data->isValidInCreatedStep())
            data->setActiveState(false);
    }
}

}
